package cn.edu.fudan.direct;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.Params;

public class TwoSideErrorFunction  implements AbstractErrorFunction {


	private static Logger logger = Logger.getLogger(TwoSideErrorFunction.class);

	// the normalization threshold
	private double nThreshold;

	// the data

	private List<DataItem> tsData_posi;
	private List<DataItem> tsData_nega;
	List<List<Long>> timepoints = new ArrayList<>();

	private TwoSideClassifiers classifiers;

	public TwoSideErrorFunction(List<DataItem> tsData_posi, List<DataItem> tsData_nega,
			List<List<Long>> timepoints, double nThreshold) {
		super();
		this.nThreshold = nThreshold;
		this.tsData_posi = tsData_posi;
		this.tsData_nega = tsData_nega;
		this.timepoints = timepoints;
		this.classifiers = new TwoSideClassifiers(tsData_posi, tsData_nega, timepoints);
	}



	@Override
	public double valueAt(Point point) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub

		double[] coords = point.toArray();
		int windowSize = Long.valueOf(Math.round(coords[0])).intValue();
		int paaSize = Long.valueOf(Math.round(coords[1])).intValue();

		if (paaSize > windowSize) {
			return 1.0d;
		}

		Params params = new Params(windowSize, paaSize, this.nThreshold);
		logger.debug(params.toString());

		// validation phase
		// Classifying...
		// is this sample correctly classified?
		double accuracy = classifiers.Classifier(params);
		double error = 1 - accuracy;
		return error;
	}

}
